package com.maiji.cloud.controller;

import com.maiji.cloud.entities.login.Label;
import com.maiji.cloud.request.BaseDataReqDto;
import com.maiji.cloud.request.ReqMetaData;
import com.maiji.cloud.request.login.*;
import com.maiji.cloud.request.shopingmall.ThumbsUpOrCollectionReqDto;
import com.maiji.cloud.response.BaseDataResDto;
import com.maiji.cloud.response.BaseMetaResDto;
import com.maiji.cloud.response.BaseResDto;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.response.login.*;
import com.maiji.cloud.service.*;
import org.eclipse.jetty.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;

@RestController
@RequestMapping("/recipe/")
public class RecipeController {

	@Autowired
	private RecipeService recipeService;
    @Autowired
    private RecipeThumbsUpOrCollectionService recipeThumbsUpOrCollectionService;

    /**
     *  获取美食推荐食谱列表(登录地区推荐优先)
     * @param token
     * @return
     */
    @PostMapping("findAllRecipeByLogin")
    public BaseMetaResDto<List<RecipeResData>> findAllRecipeByLogin(@RequestBody BaseDataReqDto baseReqMetaDto,
                                                                    @RequestHeader("maijiToken") String token) {
        ReqMetaData reqMetaData = baseReqMetaDto.getMetaData();
        if (reqMetaData == null) return new BaseMetaResDto(Status.PARAMETERERROR);
        return recipeService.findAllRecipeByLogin(token, reqMetaData);
    }

    /**
     *  获取美食推荐食谱列表_PC
     * @param baseReqMetaDto
     * @return
     */
    @PostMapping("findAllRecommendRecipes")
    public BaseMetaResDto<List<RecipeResData>> findAllRecommendRecipes(@RequestBody BaseDataReqDto baseReqMetaDto,
                                                                       @RequestHeader(name = "maijiToken", required = false) String token) {
        ReqMetaData metaData = baseReqMetaDto.getMetaData();
        if (metaData == null) return new BaseMetaResDto(Status.PARAMETERERROR);
        return recipeService.findAllRecommendRecipes(token, metaData);
    }

    /**
     *  添加或取消推荐
     * @param baseDataReqDto
     * @return
     */
    @PostMapping("addOrCancelRecommend")
    public BaseResDto addOrCancelRecommend(@RequestBody BaseDataReqDto<String> baseDataReqDto) {
        String recipeId = baseDataReqDto.getData();
        if (StringUtil.isBlank(recipeId)) return new BaseDataResDto<>(Status.PARAMETERERROR);
        return recipeService.addOrCancelRecommend(recipeId);
    }

	/**
	 *  获取食谱列表
	 * @param baseDataReqDto
	 * @return
	 */
	@PostMapping("findAllRecipes")
	public BaseMetaResDto<List<RecipeResData>> findAllRecipes(@RequestBody BaseDataReqDto<RecipeFindAllReqData> baseDataReqDto,
                                                              @RequestHeader(name = "maijiToken", required = false) String token) {
        RecipeFindAllReqData data = baseDataReqDto.getData();
		ReqMetaData reqMetaData = baseDataReqDto.getMetaData();
		if (data == null || reqMetaData == null) return new BaseMetaResDto(Status.PARAMETERERROR);
		return recipeService.findAllRecipes(data, token, reqMetaData);
	}

    /**
     *  根据食材ID相关食谱列表
     * @param baseDataReqDto
     * @return
     */
    @PostMapping("findAllRecipesByFoodId")
    public BaseMetaResDto<List<RecipeResData>> findAllRecipesByFoodId(@RequestBody BaseDataReqDto<String> baseDataReqDto,
                                                                      @RequestHeader(name = "maijiToken", required = false) String token) {
        String foodId = baseDataReqDto.getData();
        ReqMetaData reqMetaData = baseDataReqDto.getMetaData();
        if (StringUtil.isBlank(foodId) || reqMetaData == null) return new BaseMetaResDto(Status.PARAMETERERROR);
        return recipeService.findAllRecipesByFoodId(token, foodId, reqMetaData);
    }

    /**
     *  根据食谱名称或别名搜索食谱
     * @param baseDataReqDto
     * @return
     */
    @PostMapping("findAllRecipeByName")
    public BaseMetaResDto<List<RecipeResData>> findAllRecipeByName(@RequestBody BaseDataReqDto<String> baseDataReqDto,
                                                                   @RequestHeader("maijiToken") String token) {
        String recipeName = baseDataReqDto.getData();
        ReqMetaData reqMetaData = baseDataReqDto.getMetaData();
        if (recipeName == null || reqMetaData == null) return new BaseMetaResDto<>(Status.PARAMETERERROR);
        RecipeFindAllReqData recipeFindAllReqData = new RecipeFindAllReqData().setName(recipeName);
        return recipeService.findAllRecipes(recipeFindAllReqData, token, reqMetaData);
    }

	/**
	 *  根据食谱ID获取食谱详情
	 * @param baseDataReqDto
	 * @return
	 */
	@PostMapping("findRecipeById")
	public BaseDataResDto<RecipeResData> findRecipeById(@RequestBody BaseDataReqDto<String> baseDataReqDto,
                                                        @RequestHeader(name = "maijiToken", required = false) String token) {
        String recipeId = baseDataReqDto.getData();
		if (StringUtil.isBlank(recipeId)) return new BaseDataResDto(Status.PARAMETERERROR);
		return recipeService.findRecipeById(recipeId, token);
	}

    /**
     *  根据食谱ID获取食谱的标签
     * @param baseDataReqDto
     * @return
     */
    @PostMapping("findAllLabelsByRecipeId")
    public BaseDataResDto<List<Label>> findAllLabelsByRecipeId(@RequestBody BaseDataReqDto<String> baseDataReqDto) {
        String recipeId = baseDataReqDto.getData();
        if (StringUtil.isBlank(recipeId)) return new BaseDataResDto(Status.PARAMETERERROR);
        return recipeService.findAllLabelsByRecipeId(recipeId);
    }

	/**
	 *  逻辑删除食谱
	 * @param baseDataReqDto
	 * @return
	 */
	@PostMapping("delRecipesByIds")
	public BaseResDto delRecipesByIds(@RequestBody BaseDataReqDto<List<String>> baseDataReqDto) {
        List<String> data = baseDataReqDto.getData();
		if (data == null) return new BaseMetaResDto(Status.PARAMETERERROR);
		return recipeService.delRecipesByIds(data);
	}

	/**
	 *  添加食谱
	 * @param baseDataReqDto
	 * @return
	 */
	@PostMapping("addRecipe")
	public BaseResDto addRecipe(@RequestBody BaseDataReqDto<RecipeReqData> baseDataReqDto) {
        try {
            RecipeReqData data = baseDataReqDto.getData();
            if (data == null) return new BaseResDto(Status.PARAMETERERROR);
            return recipeService.addRecipe(data);
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseResDto(Status.ERROR);
        }
    }

	/**
	 *  编辑食谱
	 * @param baseDataReqDto
	 * @return
	 */
	@PostMapping("editRecipe")
	public BaseResDto editRecipe(@RequestBody BaseDataReqDto<RecipeReqData> baseDataReqDto) {
        try {
            RecipeReqData data = baseDataReqDto.getData();
            if (data == null) return new BaseResDto(Status.PARAMETERERROR);
            return recipeService.editRecipe(data);
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseResDto(Status.ERROR);
        }
    }

    /**
     *  食谱点赞/收藏或取消点赞/收藏
     * @param thumbsUpOrCollectionReqDto
     * @param token
     * @return
     */
    @PostMapping("thumbsUpOrCollectionRecipeById")
    public BaseResDto thumbsUpRecipeById(@RequestBody ThumbsUpOrCollectionReqDto thumbsUpOrCollectionReqDto, @RequestHeader("maijiToken") String token) {
        String recipeId = thumbsUpOrCollectionReqDto.getData().getUuId();
        Integer type = thumbsUpOrCollectionReqDto.getData().getType();
        if (recipeId == null || type == null) return new BaseResDto(Status.PARAMETERERROR);
        return recipeThumbsUpOrCollectionService.thumbsUpRecipeById(recipeId, type, token);
    }

    /**
     * 批量删除用户收藏食谱（空参为清空收藏）
     * @param baseDataReqDto
     * @param token
     * @return
     */
    @PostMapping("cancelCollectionRecipes")
    public BaseResDto cancelCollectionRecipes(@RequestBody BaseDataReqDto<List<String>> baseDataReqDto,
                                              @RequestHeader("maijiToken") String token) {
        List<String> recipeIds = baseDataReqDto.getData();
        return recipeThumbsUpOrCollectionService.cancelCollectionRecipes(recipeIds, token);
    }

    /**
     *  获取用户收藏食谱列表
     * @param baseReqMetaDto
     * @param token
     * @return
     */
    @PostMapping("findAllRecipeCollection")
    public BaseMetaResDto<List<RecipeResData>> findAllRecipeCollection(@RequestBody BaseDataReqDto baseReqMetaDto,
                                                                       @RequestHeader("maijiToken") String token) {
        ReqMetaData reqMetaData = baseReqMetaDto.getMetaData();
        if (reqMetaData == null) return new BaseMetaResDto<>(Status.PARAMETERERROR);
        return recipeThumbsUpOrCollectionService.findAllRecipeCollection(token, reqMetaData);
    }

    /**
     *  批量导入食谱
     * @param uploadFile
     * @return
     */
    @PostMapping("uploadRecipes")
    public BaseDataResDto<String> uploadRecipes(@RequestBody MultipartFile uploadFile) {
        try {
            if (uploadFile == null) return new BaseDataResDto(Status.ERROR);
            return recipeService.uploadRecipes(uploadFile);
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseDataResDto(Status.ERROR);
        }
    }

}
